<route lang="json">
{
  "style": {
    "navigationBarTitleText": "订单退货",
    "backgroundColorContent": "#f1f1f1"
  }
}
</route>
    
<template>
  <div class="detail" v-if="detail">
    <nut-form ref="ruleForm" :model-value="form" :rules="rules">
      <nut-cell title="交易流水号" :desc="detail.sysSeqNum" />
      <nut-cell title="交易日期" :desc="detail.transDate" />
      <nut-cell title="商户编号" :desc="detail.mchtNo" />
      <nut-cell title="终端编号" :desc="detail.terminalNo" />
      <nut-cell title="交易金额">
        <!-- :desc="detail.transAmt + ''" -->
        <template #desc>
          <nut-price :price="detail.transAmt" symbol="¥" custom-style="color: #000" />
        </template>
      </nut-cell>
      <nut-cell title="已退货金额" :desc="detail.refundedAmount + ''">
        <template #desc>
          <nut-price :price="detail.refundedAmount" symbol="¥" custom-style="color: #000" />
        </template>
      </nut-cell>
      <nut-cell title="退货标识" :desc="{'0': '正常', '1': '已退货'}[detail.rfdFlag]" />
      <nut-cell title="授权码" :desc="detail.authIdr" v-if="detail.authIdr" />
      <nut-form-item label="店长授权码" body-align="right" prop="authCode" required>
        <nut-input v-model="form.authCode" placeholder="请输入店长授权码" type="number" input-align="right" />
      </nut-form-item>
      <nut-form-item label="本次退货金额(元)" label-width="130" body-align="right" prop="refundAmount" required>
        <nut-input v-model="form.refundAmount" placeholder="请输入本次退货金额" type="digit" input-align="right" />
      </nut-form-item>
    </nut-form>
  </div>
  <div class="actions" v-if="detail">
    <div class="li-btn-tj shake_o" @click="refund">确认退货</div>
    <!-- <nut-button type="primary" block @click="refund">确认退货</nut-button> -->
  </div>
</template>

<script setup lang="ts">
import mcht from '@/api/mcht'
import { useHomeStore } from '@/store'
const {
  modifyRefund
} = storeToRefs(useHomeStore())
const ruleForm = ref()
const form = reactive({
  authCode: '',
  refundAmount: ''
})

const detail = ref<any>(null)
onLoad((options: any) => {
  if (options.sysSeqNum) {
    if (options.sysSeqNum === modifyRefund.value.sysSeqNum) {
      detail.value = modifyRefund.value
    }
  }
})


const rules = {
  authCode: [
    { required: true, message: '请输入店长授权码' },
  ],
  refundAmount: [
    { required: true, message: '请输入本次退货金额' },
  ],
}

async function refund () {
  ruleForm.value.validate().then(async ({valid}: { valid: boolean }) => {
    if (valid) {
      const { data } = await mcht.refund({
        sysSeqNum: detail.value.sysSeqNum,
        authCode: form.authCode,
        refundAmount: form.refundAmount
      })
      if (data?.status === 'success') {
        // if (data.data.trStatus === 'B3000'){
        // } else if (data.data.trStatus === 'B3003'){}
        uni.showModal({
          title: '提示',
          content: data.data.trMsg,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              detail.value = null
              form.authCode = ''
              form.refundAmount = ''
              uni.navigateBack({
                delta: 2
              })
            }
          }
        });
      }
    }
  })
}

function masktring(str) {
  if (str.length <= 18) {
    return str
  }
  let start = str.slice(0, 8)
  let end = str.slice(-8)
  return start + ' **** ' + end
}
</script>

<style lang="sass" scoped>
.actions
  width: 686rpx
  margin-left: 32rpx
:deep(.nut-cell__value)
  word-break: break-all
</style>